package westeel.dataserver.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.Charset;

/**
 * @author chengjunyu
 * @classname MyFastJsonConfig
 * @description TODO
 * @date 2020/7/25 22:57
 */
@Configuration
public class WebConfigSupportFastJson {

    @Bean
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter messageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //数据编码-UTF-8
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        //日期格式
        fastJsonConfig.setDateFormat("yyyy-MM-dd");
        fastJsonConfig.setSerializerFeatures(
                //在生成的JSON中输出类名
                //SerializerFeature.WriteClassName,
                //输出value为null的数据
                //SerializerFeature.WriteMapNullValue,
                //空集合输出[]而不是null
                //SerializerFeature.WriteNullListAsEmpty,
                //空字符串输出""而不是null
                //SerializerFeature.WriteNullStringAsEmpty,
                //生成的json格式话
                SerializerFeature.PrettyFormat
        );
        messageConverter.setFastJsonConfig(fastJsonConfig);
        return messageConverter;
    }
}